package com.bocai.freeman.view.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bocai.freeman.R;
import com.bocai.freeman.base.BaseFragment;
import com.bocai.freeman.utils.DialogHelper;
import com.bocai.freeman.utils.DividerItemDecoration;
import com.bocai.freeman.view.activitys.ApplyRefundActivity;
import com.bocai.freeman.view.activitys.EvaluateActivity;
import com.bocai.freeman.view.activitys.SkillDetailNoPayActivity;
import com.bocai.freeman.view.activitys.SkillDetailPaidActivity;
import com.bocai.freeman.view.activitys.SkillDetailcompActivity;
import com.bocai.freeman.view.adapter.OrderSkillsAdapter;
import com.orhanobut.dialogplus.DialogPlus;
import com.orhanobut.dialogplus.OnClickListener;
import com.orhanobut.dialogplus.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by win7 on 2016/5/19.
 */
public class OrderSkillFrag extends BaseFragment {
    @Bind(R.id.rv)
    RecyclerView rv;
    private List<Integer> mData;
    private OrderSkillsAdapter mOrderAdapter;


    public static final int PAY_MONEY = 5;
    public static final int CANCEL_ORDER = 6;
    public static final int COMMIT_PAY = 7;
    public static final int APPLY_REFUND = 8;
    public static final int EVALUATE = 9;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.view_all_list, null);
        ButterKnife.bind(this, view);
        initView();
        initEvent();
        return view;
    }

    @Override
    public void initEvent() {
        if (mOrderAdapter != null) {
            mOrderAdapter.setOnItemClickLitener(new OrderSkillsAdapter.OnItemClickLitener() {
                @Override
                public void onItemClick(View view, int position, int itemType) {
                    switch (itemType) {
                        case 1:
                            startActivity(new Intent(getActivity(), SkillDetailcompActivity.class));
                            showToast("item:" + itemType + "position:" + position);
                            break;
                        case 2:
                            startActivity(new Intent(getActivity(), SkillDetailcompActivity.class));
                            showToast("item:" + itemType + "position:" + position);
                            break;
                        case 3:
                            startActivity(new Intent(getActivity(), SkillDetailNoPayActivity.class));
                            showToast("item:" + itemType + "position:" + position);
                            break;
                        case 4:
                            startActivity(new Intent(getActivity(), SkillDetailPaidActivity.class));
                            showToast("item:" + itemType + "position:" + position);
                            break;
                        case COMMIT_PAY:
                            showDialog();
                            break;
                        case APPLY_REFUND:
                            startActivity(new Intent(getActivity(), ApplyRefundActivity.class));
                            break;
                        case PAY_MONEY:
                            showDialogPlus();
                            break;
                        case CANCEL_ORDER:
                            showCancelDialog();
                            break;
                        case EVALUATE:
                            startActivity(new Intent(getActivity(), EvaluateActivity.class));
                            break;

                    }
                }


            });
        }
    }

    private void showCancelDialog() {
        DialogHelper.cancelOrderDialog(getActivity(), new DialogHelper.OnLitener() {
            @Override
            public void onConfirm(AlertDialog dialog, TextView textView) {
                dialog.dismiss();
            }

            @Override
            public void onCancel(AlertDialog dialog, TextView textView) {
                dialog.dismiss();
            }
        }).show();
    }

    private void showDialogPlus() {
        final DialogPlus dialog = DialogPlus.newDialog(getActivity())
                .setContentHolder(new ViewHolder(R.layout.dialogplus_order_pay))
                .setGravity(Gravity.BOTTOM)
                .setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(DialogPlus dialog, View view) {
                        switch (view.getId()) {
                            case R.id.img_dismiss:
                                break;
                            case R.id.btn_commit:
                                break;
                        }
                        dialog.dismiss();
                    }
                })
                .setExpanded(false)
                .setCancelable(true)
                .create();
        dialog.show();
    }

    private void showDialog() {
        DialogHelper.commonDialog(getActivity(), "一旦确认支付，您之前托管在平台的款项将立刻打给对方",
                "确认付款", "取消", new DialogHelper.OnLitener() {
                    @Override
                    public void onConfirm(AlertDialog dialog, TextView textView) {
                        dialog.dismiss();
                    }

                    @Override
                    public void onCancel(AlertDialog dialog, TextView textView) {
                        dialog.dismiss();
                    }
                }).show();
    }

    private void initView() {
        mData = new ArrayList<>();
        mData.add(0);
        mData.add(1);
        mData.add(0);
        mData.add(0);
        mData.add(0);
        mData.add(0);
        mData.add(2);
        mData.add(2);
        mData.add(0);
        mData.add(3);
        mData.add(0);
        mData.add(3);
        mData.add(0);
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        rv.setLayoutManager(manager);
        rv.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
        mOrderAdapter = new OrderSkillsAdapter(mData, getActivity());
        rv.setAdapter(mOrderAdapter);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
